import axios from 'axios'     //引入
import { Message } from 'element-ui'
import cookies from "vue-cookies";
import router from "../router"

// let baseURL = 'http://www.yousubao-01.com'  //正式服域名 三期
let baseURL = 'https://www.yousubao-01.com'  //正式服域名 三期
// let baseURL = 'https://test.xsy123.com'  //正式服域名 三期
// let baseURL = 'https://resource.xiaoweitec.com/you_su_bao'  //正式服域名
// let baseURL = 'https://resource.xiaoweitec.com/you_su_bao'   //测试服域名
// let baseURL = 'http://127.0.0.1:8080/you_su_bao'

let config = {
  baseURL: baseURL,
  timeout: 30000       //设置最大请求时间
}
const _axios = axios.create(config)


/* 请求之后的操作 */
_axios.interceptors.response.use(res => res,  error => {  

  // console.log("error",error);
  if(error.response.data.message == "此账号在其他设备上登录，此客户端已退出登录，如需多人使用请申请子账号" || error.response.data.message =="账号已被禁用"){
 
    setTimeout(() => {
      router.push({
        name:"login"
      })
    }, 3000);
  }

  if(error.response.data.message == "token不能为空"){
    error.response.data.message = "当前token值为空，请重新登录"
    setTimeout(() => {
      router.push({
        name:"login"
      })
    }, 2000);
  }
    
  if(error.response.data.message == "同一个供应信息十分钟内只能更新一次" || error.response.data.message =="只有优塑会员才能使用一键更新功能!")
  {
    error.response.data.message == ""
  }else{
    Message.error(error.response.data.message)
    return Promise.reject(error);
  }


  if (error.response.data.message =="该账号在别处登录或者登录信息已过期,请重新登录") {
    cookies.set("you_su_bao", "");
    setTimeout(() => {
      router.push({
        name:"login"
      })
    }, 3000);
  }
 }
);

//封装post,get方法
const http = {
  // get(url = '', params = {}) {
  //   return new Promise((resolve, reject) => {
  //     _axios({
  //       url,
  //       params,
  //       headers: {
  //         'Content-Type': 'application/json;charset=UTF-8',
  //       },
  //       method: 'GET'
  //     }).then(res => {
  //       resolve(res.data)
  //       return res
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },
  post(url = '', params = {}) {
    let header = {};
    if (cookies.get("you_su_bao") && !/auth/.test(url)){
      header.Authorization = `Bearer ${cookies.get("you_su_bao")}`
    }
    return new Promise((resolve) => {
      _axios({
        url,
        data: params,
        headers: {
          'X-Requested-With': 'XMLHttpRequest',
          'Content-Type': 'application/json;',
          ...header
        },
        method: 'POST'
      }).then(res => {
        resolve(res.data)
      }).catch(error => {
        // console.log(error)
        resolve(error)
      })
    })
  }
}


export default http


